home *** CD-ROM | disk | FTP | other *** search
/ Aminet 43 / Aminet 43 (2001)(GTI - Schatztruhe)[!][Jun 2001].iso / Aminet / dev / moni / SystemViewer.lha / Source / SysMemory.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-19  |  16.0 KB  |  635 lines

  1. /****h *SysMemory.c ************************************************
  2. ** 
  3. ** NAME
  4. **    SysMemory.c
  5. **
  6. ** DESCRIPTION
  7. **    Display parameters associated with system memory.
  8. **
  9. **  GUI Designed by : Jim Steichen
  10. ********************************************************************
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. #include <exec/types.h>
  17. #include <exec/execbase.h>
  18. #include <exec/memory.h>
  19.  
  20. #include <AmigaDOSErrs.h>
  21.  
  22. #include <intuition/intuition.h>
  23. #include <intuition/classes.h>
  24. #include <intuition/classusr.h>
  25. #include <intuition/imageclass.h>
  26. #include <intuition/gadgetclass.h>
  27.  
  28. #include <libraries/gadtools.h>
  29.  
  30. #include <graphics/displayinfo.h>
  31. #include <graphics/gfxbase.h>
  32.  
  33. #include <clib/exec_protos.h>
  34. #include <clib/intuition_protos.h>
  35. #include <clib/gadtools_protos.h>
  36. #include <clib/graphics_protos.h>
  37. #include <clib/utility_protos.h>
  38. #include <clib/diskfont_protos.h>
  39.  
  40. #include "CPGM:GlobalObjects/CommonFuncs.h"
  41.  
  42. #include "SysLists.h"
  43.  
  44. #define MLV      0
  45. #define Update   1
  46. #define Priority 2
  47. #define Cancel   3
  48. #define More     4
  49.  
  50. #define MEM_CNT  5
  51.  
  52. #define LISTVIEWGADGET MemGadgets[ MLV ]
  53.  
  54. PRIVATE char ver[] = "$VER: SysMemory 1.0 (12/20/1999) by J.T. Steichen";
  55.  
  56. PRIVATE struct Window       *MemWnd   = NULL;
  57. PRIVATE struct Gadget       *MemGList = NULL;
  58. PRIVATE struct IntuiMessage  MemMsg;
  59. PRIVATE struct Gadget       *MemGadgets[ MEM_CNT ];
  60.  
  61. PRIVATE UWORD MemLeft   = 0;
  62. PRIVATE UWORD MemTop    = 16;
  63. PRIVATE UWORD MemWidth  = 632;
  64. PRIVATE UWORD MemHeight = 228;
  65. PRIVATE UBYTE *MemWdt   = (UBYTE *) "System Memory Info:";
  66.  
  67. PRIVATE struct TextFont *MFont = NULL;
  68.  
  69. #define MAXMEMS  10
  70.  
  71. PRIVATE struct MinList MemList;
  72. PRIVATE struct Node    MemNode;
  73. PRIVATE struct Node    MemNodes[ MAXMEMS       ] = { NULL, };
  74. PRIVATE UBYTE          NodeStrs[  MAXMEMS * 80 ] = "";
  75.  
  76. PRIVATE UWORD MemGTypes[] = {
  77.  
  78.    LISTVIEW_KIND, BUTTON_KIND, BUTTON_KIND,
  79.    BUTTON_KIND,   BUTTON_KIND
  80. };
  81.  
  82. PRIVATE int MLVClicked(      int whichitem );
  83. PRIVATE int UpdateClicked(   int dummy     );
  84. PRIVATE int PriorityClicked( int dummy     );
  85. PRIVATE int CancelClicked(   int dummy     );
  86. PRIVATE int MoreClicked(     int dummy     );
  87.  
  88. PRIVATE struct NewGadget MemNGad[] = {
  89.  
  90.      2,   3, 627, 200,                 NULL, NULL, MLV, 
  91.    0, NULL, (APTR) MLVClicked,
  92.    
  93.      2, 205,  71,  17, (UBYTE *) "_Update",  NULL, Update, 
  94.    PLACETEXT_IN, NULL, (APTR) UpdateClicked,
  95.  
  96.    396, 205,  72,  17, (UBYTE *) "Priority", NULL, Priority, 
  97.    PLACETEXT_IN, NULL, (APTR) PriorityClicked,
  98.  
  99.    554, 205,  72,  17, (UBYTE *) "_Cancel",  NULL, Cancel, 
  100.    PLACETEXT_IN, NULL, (APTR) CancelClicked,
  101.  
  102.     82, 205,  71,  17, (UBYTE *) "_More",    NULL, More, 
  103.    PLACETEXT_IN, NULL, (APTR) MoreClicked
  104. };
  105.  
  106. PRIVATE ULONG MemGTags[] = {
  107.  
  108.    GTLV_ShowSelected, NULL, LAYOUTA_Spacing, 2, TAG_DONE,
  109.    GT_Underscore,      '_', TAG_DONE,
  110.    GA_Disabled,       TRUE, TAG_DONE,
  111.    GT_Underscore,      '_', TAG_DONE,
  112.    GT_Underscore,      '_', GA_Disabled, TRUE, TAG_DONE
  113.  
  114. };
  115.  
  116. /* -------------------------------------------------------------- */
  117.  
  118. PRIVATE struct MemHeader *GetMemoryHeader( ULONG address )
  119. {
  120.    IMPORT struct ExecBase *SysBase;
  121.  
  122.    struct MemHeader *addr = NULL;   
  123.    struct List      *lptr = NULL;
  124.  
  125.    Forbid();
  126.  
  127.      lptr  = &SysBase->MemList;
  128.      addr  = (struct MemHeader *) lptr->lh_Head;
  129.      
  130.      while ((addr != (struct MemHeader *) address) && (addr != NULL))
  131.         addr = (struct MemHeader *) addr->mh_Node.ln_Succ;
  132.  
  133.    Permit();
  134.  
  135.    if (addr != NULL)
  136.       return( addr );
  137.    else
  138.       return( NULL );
  139. }
  140.  
  141. PRIVATE ULONG GetMemoryLargest( int memtype )
  142. {
  143.    return( AvailMem( memtype | MEMF_LARGEST ) );
  144. }
  145.  
  146. PRIVATE ULONG GetMemorySize( int memtype )
  147. {
  148.    return( AvailMem( memtype | MEMF_TOTAL ) );
  149. }
  150.  
  151. PRIVATE int InitializeMemoryList( void )
  152. {
  153.    IMPORT struct ExecBase *SysBase;
  154.  
  155.    struct List      *lptr = &SysBase->MemList;
  156.    struct MemHeader *addr = (struct MemHeader *) lptr->lh_Head;   
  157.    struct MemHeader *mp   = GetMemoryHeader( (ULONG) addr );
  158.    ULONG             lrg  = GetMemoryLargest( addr->mh_Attributes );
  159.    ULONG             max  = 0L; 
  160.    ULONG             tmax = 0L, tuse = 0L, tfree = 0L;
  161.    int               i, rval = 0;
  162.       
  163.    HideListFromView( LISTVIEWGADGET, MemWnd );
  164.  
  165.    max = GetMemorySize( addr->mh_Attributes );
  166.  
  167.    for (i = 1; i <= MAXMEMS; i++)
  168.       {
  169.       // i = 1 so we don't overwrite the column headings.
  170.  
  171.       sprintf( &NodeStrs[i * 80], 
  172.                "%08LX %4d %08LX %08LX %5uK %5uK  %5uK %5uK  %-20.20s",
  173.                mp, mp->mh_Node.ln_Pri, mp->mh_Lower, mp->mh_Upper,
  174.                max / 1000, 
  175.                (max - mp->mh_Free) / 1000, 
  176.                mp->mh_Free / 1000, 
  177.                lrg / 1000,
  178.                mp->mh_Node.ln_Name
  179.              );
  180.  
  181.       tmax  += max;
  182.       tuse  += max - mp->mh_Free;
  183.       tfree += mp->mh_Free;
  184.  
  185.       rval++;
  186.             
  187.       if (addr != NULL) 
  188.          {
  189.          addr = (struct MemHeader *) addr->mh_Node.ln_Succ;
  190.  
  191.          if (strcmp( "chip memory", addr->mh_Node.ln_Name ) == 0)
  192.             {
  193.             mp   = GetMemoryHeader( (ULONG) addr );
  194.             lrg  = GetMemoryLargest( addr->mh_Attributes );
  195.             max  = GetMemorySize( addr->mh_Attributes );
  196.  
  197.             sprintf( &NodeStrs[(i + 1) * 80], 
  198.                      "%08LX %4d %08LX %08LX %5uK %5uK  %5uK %5uK  %-20.20s",
  199.                      mp, mp->mh_Node.ln_Pri, mp->mh_Lower, mp->mh_Upper,
  200.                      max / 1000, 
  201.                      (max - mp->mh_Free) / 1000, 
  202.                      mp->mh_Free / 1000, 
  203.                      lrg / 1000,
  204.                      mp->mh_Node.ln_Name
  205.                    );
  206.  
  207.             tmax  += max;
  208.             tuse  += max - mp->mh_Free;
  209.             tfree += mp->mh_Free;
  210.             rval++;
  211.                  
  212.             break; // chip memory MemHeader is the last valid MemHeader!
  213.             }
  214.  
  215.          mp   = GetMemoryHeader( (ULONG) addr );
  216.          lrg  = GetMemoryLargest( addr->mh_Attributes );
  217.          max  = GetMemorySize( addr->mh_Attributes );
  218.          }
  219.       else
  220.          break;
  221.       }
  222.  
  223.    if ((i + 2) < MAXMEMS)
  224.       sprintf( &NodeStrs[ 80 * (i + 2)], 
  225.                "                       TOTALS:  %5uK %5uK  %5uK",
  226.                tmax / 1000, tuse / 1000, tfree / 1000
  227.              );
  228.  
  229.    // Turn on the list view gadget again:   
  230.    GT_SetGadgetAttrs( LISTVIEWGADGET, MemWnd, NULL,
  231.                       GTLV_Labels,       &MemList,
  232.                       GTLV_Selected,     0, 
  233.                       GTLV_MaxPen,       255,
  234.                       GTLV_ItemHeight,   12,
  235.                       TAG_END
  236.                     );
  237.    return( rval );
  238. }
  239.  
  240. PRIVATE ULONG CurrentMem  = 0L;
  241. PRIVATE int   CurrentType = MEMF_CHIP;
  242.  
  243. PRIVATE char mt[80], *modtitle = &mt[0];
  244.  
  245. PRIVATE int MLVClicked( int whichitem )
  246. {
  247.    ULONG addr = 0L;
  248.  
  249.    if (whichitem == 0)
  250.       {
  251.       // Disable buttons because user selected the title item:
  252.  
  253.       GT_SetGadgetAttrs( MemGadgets[ More ], MemWnd, NULL,
  254.                          GA_Disabled, TRUE, TAG_DONE 
  255.                        );
  256.  
  257.       GT_SetGadgetAttrs( MemGadgets[ Priority ], MemWnd, NULL,
  258.                          GA_Disabled, TRUE, TAG_DONE 
  259.                        );
  260.    
  261.       SetWindowTitles( MemWnd, MemWdt, (UBYTE *) -1 );
  262.  
  263.       CurrentMem = 0L;
  264.  
  265.       return( (int) TRUE );
  266.       }
  267.  
  268.    // Now get address from the item:
  269.    
  270.    (void) stch_l( MemNodes[ whichitem ].ln_Name, (long *) &addr );
  271.    CurrentMem = (ULONG) addr;
  272.  
  273.    if (addr == NULL)
  274.       {
  275.       GT_SetGadgetAttrs( MemGadgets[ More ], MemWnd, NULL,
  276.                          GA_Disabled, TRUE, TAG_DONE 
  277.                        );
  278.  
  279.       GT_SetGadgetAttrs( MemGadgets[ Priority ], MemWnd, NULL,
  280.                          GA_Disabled, TRUE, TAG_DONE 
  281.                        );
  282.    
  283.       SetWindowTitles( MemWnd, MemWdt, (UBYTE *) -1 );
  284.  
  285.       return( (int) TRUE );
  286.       }    
  287.    else
  288.       {
  289.       sprintf( modtitle, "%s  You Selected:  %08LX", MemWdt, addr );
  290.       SetWindowTitles( MemWnd, modtitle, (UBYTE *) -1 );
  291.       }
  292.  
  293.    // Enable buttons because user sel'd a Memory Block from the ListView:
  294.  
  295.    GT_SetGadgetAttrs( MemGadgets[ More ], MemWnd, NULL,
  296.                       GA_Disabled, FALSE, TAG_DONE 
  297.                     );
  298.  
  299.    GT_SetGadgetAttrs( MemGadgets[ Priority ], MemWnd, NULL,
  300.                       GA_Disabled, FALSE, TAG_DONE 
  301.                     );
  302.  
  303.    return( (int) TRUE );
  304. }
  305.  
  306. PRIVATE int UpdateClicked( int dummy )
  307. {
  308.    int i;
  309.    
  310.    for (i = 1; i <= MAXMEMS; i++)
  311.        NodeStrs[ i * 80 ] = '\0'; // Kill old ListView strings.
  312.  
  313.    // Disable buttons until user selects an item again:
  314.  
  315.    GT_SetGadgetAttrs( MemGadgets[ More ], MemWnd, NULL,
  316.                       GA_Disabled, TRUE, TAG_DONE 
  317.                     );
  318.  
  319.    GT_SetGadgetAttrs( MemGadgets[ Priority ], MemWnd, NULL,
  320.                       GA_Disabled, TRUE, TAG_DONE 
  321.                     );
  322.    
  323.    SetWindowTitles( MemWnd, MemWdt, (UBYTE *) -1 );
  324.  
  325.    CurrentMem = 0L;
  326.  
  327.    if (InitializeMemoryList() < 0)
  328.       {
  329.       // No Memory Blocks known to SysBase!!
  330.       SetReqButtons( "OKAY" );
  331.  
  332.       (void) Handle_Problem( "No Memory Blocks found!", 
  333.                              "OS ERROR??", NULL
  334.                            );
  335.  
  336.       SetReqButtons( "CONTINUE|ABORT" );
  337.       }  
  338.  
  339.    GT_RefreshWindow( MemWnd, NULL );
  340.  
  341.    return( (int) TRUE );
  342. }
  343.  
  344. PRIVATE int PriorityClicked( int dummy )
  345. {
  346.    (void) Handle_Problem( "NOT Implemented yet!", 
  347.                           "User Information:", NULL
  348.                         );
  349.    
  350.    return( (int) TRUE );
  351. }
  352.  
  353. PRIVATE void CloseMemWindow( void )
  354. {
  355.    if (MemWnd != NULL)
  356.       {
  357.       CloseWindow( MemWnd );
  358.       MemWnd = NULL;
  359.       }
  360.  
  361.    if (MemGList != NULL)
  362.       {
  363.       FreeGadgets( MemGList );
  364.       MemGList = NULL;
  365.       }
  366.  
  367.    if (MFont != NULL)
  368.       {
  369.       CloseFont( MFont );
  370.       MFont = NULL;
  371.       }
  372.  
  373.    return;
  374. }
  375.  
  376. PRIVATE int MemCloseWindow( void )
  377. {
  378.    CloseMemWindow();
  379.    return( (int) FALSE );
  380. }
  381.  
  382. PRIVATE int MoreClicked( int dummy )
  383. {
  384.    IMPORT int ViewMemory( int type, ULONG startaddress, ULONG endaddress );
  385.    
  386.    (void) Handle_Problem( "NOT Implemented yet!", "USER Info:", NULL );
  387.  
  388. /*
  389.    if (ViewMemory( CurrentType, CurrentMem, ???? ) < 0)
  390.       {
  391.       // User asked to abort in ViewMemory()!
  392.       return( MemCloseWindow() );
  393.       }
  394. */
  395.    return( (int) TRUE );
  396. }
  397.  
  398.  
  399. PRIVATE int CancelClicked( int dummy )
  400. {
  401.    return( MemCloseWindow() );
  402. }
  403.  
  404. PRIVATE BOOL MemVanillaKey( int whichkey )
  405. {
  406.    BOOL rval = TRUE;
  407.    
  408.    switch (whichkey)
  409.       {
  410.       case 'c':
  411.       case 'C':
  412.          rval = CancelClicked( 0 );
  413.          break;
  414.                
  415.       case 'u':
  416.       case 'U':
  417.          rval = UpdateClicked( 0 );
  418.          break;
  419.          
  420.       case 'm':
  421.       case 'M':
  422.          rval = MoreClicked( 0 );
  423.          break;
  424.       }
  425.   
  426.   return( rval );
  427. }
  428.  
  429.  
  430. PRIVATE int OpenMemWindow( void )
  431. {
  432.    struct NewGadget  ng;
  433.    struct Gadget    *g;
  434.    UWORD             lc, tc;
  435.    UWORD             wleft = MemLeft, wtop = MemTop, ww, wh;
  436.  
  437.    ComputeFont( Scr, Font, &CFont, MemWidth, MemHeight );
  438.  
  439.    ww = ComputeX( CFont.FontX, MemWidth );
  440.    wh = ComputeY( CFont.FontY, MemHeight );
  441.  
  442.    if ((wleft + ww + CFont.OffX + Scr->WBorRight) > Scr->Width)
  443.       wleft = Scr->Width - ww;
  444.  
  445.    if ((wtop + wh + CFont.OffY + Scr->WBorBottom) > Scr->Height)
  446.       wtop = Scr->Height - wh;
  447.  
  448.    if ( !(MFont = OpenDiskFont( Font )))
  449.       return( -5 );
  450.  
  451.    if ( !(g = CreateContext( &MemGList )))
  452.       return( -1 );
  453.  
  454.    for (lc = 0, tc = 0; lc < MEM_CNT; lc++)
  455.       {
  456.       CopyMem( (char *) &MemNGad[lc], (char *) &ng, 
  457.                (long) sizeof( struct NewGadget )
  458.              );
  459.  
  460.       ng.ng_VisualInfo = VisualInfo;
  461.       ng.ng_TextAttr   = Font;
  462.       ng.ng_LeftEdge   = CFont.OffX + ComputeX( CFont.FontX, 
  463.                                                 ng.ng_LeftEdge
  464.                                               );
  465.  
  466.       ng.ng_TopEdge    = CFont.OffY + ComputeY( CFont.FontY, 
  467.                                                 ng.ng_TopEdge
  468.                                               );
  469.  
  470.       ng.ng_Width      = ComputeX( CFont.FontX, ng.ng_Width );
  471.       ng.ng_Height     = ComputeY( CFont.FontY, ng.ng_Height );
  472.  
  473.       MemGadgets[lc] = g = CreateGadgetA( (ULONG) MemGTypes[lc], 
  474.                              g, 
  475.                              &ng, 
  476.                              (struct TagItem *) &MemGTags[tc] );
  477.  
  478.       while (MemGTags[tc] != NULL)
  479.          tc += 2;
  480.  
  481.       tc++;
  482.  
  483.       if (NOT g)
  484.          return( -2 );
  485.       }
  486.  
  487.    if ( !(MemWnd = OpenWindowTags( NULL,
  488.  
  489.                      WA_Left,        wleft,
  490.                      WA_Top,         wtop,
  491.                      WA_Width,       ww + CFont.OffX + Scr->WBorRight,
  492.                      WA_Height,      wh + CFont.OffY + Scr->WBorBottom,
  493.                      
  494.                      WA_IDCMP,       LISTVIEWIDCMP | BUTTONIDCMP 
  495.                        | IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW
  496.                        | IDCMP_VANILLAKEY,
  497.                        
  498.                      WA_Flags,       WFLG_DRAGBAR | WFLG_DEPTHGADGET 
  499.                        | WFLG_CLOSEGADGET | WFLG_SMART_REFRESH 
  500.                        | WFLG_ACTIVATE | WFLG_RMBTRAP,
  501.  
  502.                      WA_Gadgets,     MemGList,
  503.                      WA_Title,       MemWdt,
  504.                      WA_ScreenTitle, "System Info",
  505.                      TAG_DONE )) 
  506.       )
  507.       return( -4 );
  508.  
  509.    GT_RefreshWindow( MemWnd, NULL );
  510.  
  511.    return( 0 );
  512. }
  513.  
  514. PRIVATE int HandleMemIDCMP( void )
  515. {
  516.    struct IntuiMessage  *m;
  517.    int                 (*func)( int code );
  518.    BOOL                  running = TRUE;
  519.  
  520.    while (running == TRUE)
  521.       {
  522.       if ((m = GT_GetIMsg( MemWnd->UserPort )) == NULL)
  523.          {
  524.          (void) Wait( 1L << MemWnd->UserPort->mp_SigBit );
  525.          continue;
  526.          }
  527.  
  528.       CopyMem( (char *) m, (char *) &MemMsg, 
  529.                (long) sizeof( struct IntuiMessage )
  530.              );
  531.  
  532.       GT_ReplyIMsg( m );
  533.  
  534.       switch (MemMsg.Class)
  535.          {
  536.          case IDCMP_REFRESHWINDOW:
  537.             GT_BeginRefresh( MemWnd );
  538.             GT_EndRefresh( MemWnd, TRUE );
  539.             break;
  540.  
  541.          case IDCMP_CLOSEWINDOW:
  542.             running = MemCloseWindow();
  543.             break;
  544.  
  545.          case IDCMP_VANILLAKEY:
  546.             running = (int) MemVanillaKey( MemMsg.Code );
  547.             break;
  548.  
  549.          case IDCMP_GADGETUP:
  550.          case IDCMP_GADGETDOWN:
  551.             func = (void *) ((struct Gadget *)MemMsg.IAddress)->UserData;
  552.             if (func != NULL)
  553.                running = func( MemMsg.Code );
  554.             break;
  555.          }
  556.       }
  557.  
  558.    return( running );
  559. }
  560.  
  561. PUBLIC int HandleMemoryLV( void )
  562. {
  563.    int i = 0;
  564.  
  565.    if (SetupSystemList( &OpenMemWindow ) < 0)
  566.       {
  567.       (void) Handle_Problem( "Couldn't open a System ListViewer!", 
  568.                              "Allocation Problem:", NULL 
  569.                            );
  570.       return( -1 );
  571.       }
  572.  
  573.    // Disable buttons until user selects an item from the ListView:
  574.  
  575.    GT_SetGadgetAttrs( MemGadgets[ More ], MemWnd, NULL,
  576.                       GA_Disabled, TRUE, TAG_DONE 
  577.                     );
  578.  
  579.    GT_SetGadgetAttrs( MemGadgets[ Priority ], MemWnd, NULL,
  580.                       GA_Disabled, TRUE, TAG_DONE 
  581.                     );
  582.    
  583.    MemNode.ln_Succ = (struct Node *) MemList.mlh_Tail;
  584.    MemNode.ln_Pred = (struct Node *) MemList.mlh_Head;
  585.    MemNode.ln_Type = 0;
  586.    MemNode.ln_Pri  = MAXMEMS;
  587.    MemNode.ln_Name = "Address   Pri Lower    Upper    Maximum In-Use"
  588.                      " Free   Largest Name";
  589.  
  590.    MemNodes[0]     = MemNode;
  591.  
  592.    SetNotifyWindow( MemWnd ); // For Handle_Problem().
  593.   
  594.    // MemNodes[0] is already set up:
  595.  
  596.    for (i = 1; i <= MAXMEMS; i++)
  597.        {
  598.        MemNodes[i].ln_Name = &NodeStrs[ i * 80 ];
  599.        MemNodes[i].ln_Pri  = MAXMEMS - i;
  600.        }     
  601.  
  602.    NewList( (struct List *) &MemList );
  603.  
  604.    for (i = 0; i < MAXMEMS; i++)
  605.       Enqueue( (struct List *) &MemList, &MemNodes[i] );
  606.  
  607.    (void) InitializeMemoryList();
  608.  
  609.    GT_SetGadgetAttrs( LISTVIEWGADGET, MemWnd, NULL,
  610.                       GTLV_Labels,       &MemList,
  611.                       GTLV_Selected,     0, 
  612.                       GTLV_MaxPen,       255,
  613.                       GTLV_ItemHeight,   12,
  614.                       TAG_END
  615.                     );
  616.  
  617.    GT_RefreshWindow( MemWnd, NULL );
  618.  
  619.    (void) HandleMemIDCMP();
  620.    
  621.    ShutdownSystemList();
  622.    return( 0 );
  623. }
  624.  
  625. #ifdef DEBUG
  626.  
  627. PUBLIC int main( void )
  628. {
  629.    return( HandleMemoryLV() );
  630. }
  631.  
  632. #endif
  633.  
  634. /* --------------------- END of SysMemory.c file! --------------- */
  635.